package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public PageInfo<Travel> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<Travel>(list);
    }

    @Override
    public void saveOrUpdate(Travel entity) {
        if(entity.getId() == null){
            Date d = new Date();
            entity.setCreateTime(d);
            entity.setLastUpdateTime(d);
            entity.setCollect(0);

            travelMapper.insert(entity);
            //同步保存游记的内容
            TravelContent travelContent = entity.getTravelContent();
            travelContent.setId(entity.getId());
            //主键关联
            contentMapper.insert(travelContent);
        }else{
            entity.setLastUpdateTime(new Date());
            travelMapper.updateByPrimaryKey(entity);
            TravelContent content = entity.getTravelContent();
            content.setId(entity.getId());
            contentMapper.updateByPrimaryKey(content);
        }
    }

    @Override
    public Travel get(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        TravelContent content = contentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(content);
        return travel;
    }

    @Override
    public String getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id).getContent();
    }

    @Override
    public void changeState(Long id, Integer state) {
        if(state == Travel.STATE_REFUSE){
            travelMapper.updateState(id,state,null);
        }else if(state ==  Travel.STATE_RELEASE){
            travelMapper.updateState(id,state,new Date());
        }
    }

    @Override
    public PageInfo<Travel> queryForTravels(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    //设置收藏collect状态
    public void updateCollectById(Long id, Integer collect, Long userId) {
        //根据id设置状态为收藏/未收藏
        travelMapper.updateCollectById(id, collect);
        Travel travel = travelMapper.selectByPrimaryKey(id);
        //判断是收藏操作还是取消收藏操作
        if (collect == 1){
            Collect travelCollect = new Collect();
            travelCollect.setUserId(userId);
            travelCollect.setType(3);
            travelCollect.setCollectId(travel.getId());
            travelCollect.setTitle(travel.getTitle());
            travelCollect.setCoverUrl(travel.getCoverUrl());
            travelCollect.setCollectTime(new Date());
            collectMapper.insert(travelCollect);
        } else {
            Collect travelCollect = new Collect();
            travelCollect.setUserId(userId);
            travelCollect.setType(3);
            travelCollect.setCollectId(travel.getId());
            collectMapper.deleteByPrimaryKey(travelCollect);
        }
    }
}
